package com.budayi.controller;

import com.alibaba.fastjson.JSON;
import com.budayi.common.cache.RedisCacheChannel;
import com.budayi.common.constants.BasicConstants;
import com.budayi.common.constants.CookieConstants;
import com.budayi.common.utils.JsonResult;
import com.budayi.common.utils.MD5;
import com.budayi.common.utils.ObjectUtils;
import com.budayi.common.utils.StringUtils;
import com.budayi.common.utils.web.WebUtils;
import com.budayi.entity.Menu;
import com.budayi.entity.SysUser;
import com.budayi.service.SysUserService;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author LIBOFEI
 * @ClassName LoginController
 * @description 登录控制器
 * @date 2021/4/30 13:43
 * @Version 1.0
 **/
@RestController
public class LoginController {

    private static Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private BasicConstants basicConstants;
    @Autowired
    private RedisCacheChannel redisCacheChannel;

    @RequestMapping("/sys/login")
    public JsonResult sysUserLogin(HttpServletRequest request,
                                   HttpServletResponse response,
                                   String username,
                                   String password){
        try{
            SysUser user = new SysUser();
            user.setUsername(username);
            List<SysUser> sysUserList = sysUserService.queryObjectListByCondition(user);
            if(ObjectUtils.isNull(sysUserList)){
                return JsonResult.failure("用户名不存在");
            }
            SysUser item = sysUserList.get(0);
            if(!MD5.md5(password).equals(item.getPassword())){
                return JsonResult.failure("密码错误");
            }
            WebUtils.deleteCookieDomain(request,response, CookieConstants.ADMIN_COOKIE_ID,basicConstants.getDomain());
            String uuid = StringUtils.createUUID().replace("-", "");
            JsonParser jsonParser = new JsonParser();
            JsonObject jsonObject  = jsonParser.parse(new Gson().toJson(sysUserList.get(0))).getAsJsonObject();
            redisCacheChannel.set(uuid,jsonObject.toString(),9000);
            WebUtils.setCookie(response,CookieConstants.ADMIN_COOKIE_ID, uuid, 1,basicConstants.getDomain());
            return JsonResult.success(uuid);
        }catch (Exception e){
            logger.error("LoginController--sysUserLogin()",e);
            return JsonResult.failure("系统异常");
        }
    }


    @RequestMapping("/sys/exit")
    public JsonResult sysUserExit(HttpServletRequest request,HttpServletResponse response){
        try{
            String value = WebUtils.getCookie(request,CookieConstants.ADMIN_COOKIE_ID);
            WebUtils.deleteCookieDomain(request,response,value,basicConstants.getDomain());
            redisCacheChannel.del(value);
//            Object o = redisCacheChannel.get(value);
//            SysUser sysUser = JSON.parseObject(o.toString(),SysUser.class);
            return JsonResult.success("操作成功");
        }catch (Exception e){
            logger.error("LoginController--sysUserExit()",e);
            return JsonResult.failure("系统异常");
        }
    }
}
